package com.ysd.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.Service.ModulesServcie;
import com.ysd.Service.RolesService;
import com.ysd.Service.UserInfoService;
import com.ysd.entity.Department;
import com.ysd.entity.Modules;
import com.ysd.entity.UserInfo;
import com.ysd.entity.roles;
import com.ysd.util.ResultInfoUtil;
import com.ysd.util.Treemodule;
@RestController
@RequestMapping("/users")
@CrossOrigin
public class UsersController {
	@Autowired
	private UserInfoService usersService;
	@Autowired
	private ModulesServcie modulesService;
	@Autowired
	private RolesService rolesService;
	@GetMapping("/getAll")
	public ResultInfoUtil findAllUsers(UserInfo u,Integer limit,Integer page,String did) {
		Pageable pageable =PageRequest.of(page-1, limit);
		Page<UserInfo> us =usersService.findAllUsers(u, did, pageable);
		return new ResultInfoUtil().success(us.getContent(),us.getTotalElements());
	}
	/**
	 * 删除
	 * @param uid
	 * @return
	 */
	@DeleteMapping("/deleteUsers")
	public String deleteUsers(String uid) {
		UserInfo users =new UserInfo();
		users.setUid(uid);
		try {
			usersService.deleteUsers(users);
			return "删除成功";
		} catch (Exception e) {
			// TODO: handle exception
			return "删除失败";
		}
	}
	/**
	 * 添加
	 * @param s
	 * @param did
	 * @return
	 */
	@PostMapping("/addUsers")
	public UserInfo addUsers(UserInfo s,String did) {
		Department dep =new Department();
		dep.setDid(did);
		s.setDepartment(dep);
		s.setCreateTime(new Date()); 
		UUID.randomUUID().toString();
		s.setUid(UUID.randomUUID().toString());
		s.setCreateTime(new Date());
		return usersService.addAndUpdate(s);
	}
	/** 
	 * 修改
	 * @param s 
	 * @param did
	 * @return
	 */
	@PutMapping("/updateUsers")
	public UserInfo updateUsers(UserInfo s,String did) {
		Department dep =new Department();
		dep.setDid(did);
		s.setDepartment(dep);
		return usersService.addAndUpdate(s);
	}
	/**
	 * 重置密码
	 * @param uid
	 * @return
	 */
	@RequestMapping("/resetPasscheck")
	public Integer resetPasscheck(String uid) {
		return usersService.ResetPasscheck(uid);
	}
	/**
	 * 判定是否姓名重复
	 */
	@GetMapping("/pdname")
	public Integer pdname(String uname) {
		try {
			usersService.pduname(uname);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}
	
	//---------------------------------------------------------------------
		/**
		 * 查询登录用户的模块信息并返回树型结构
		 * @param id
		 * @return
		 */
		@GetMapping("/modules") 
		public Map<Object, Object> findUserById(String uid){
			System.out.println(uid+"uid..........");
			
			Map<Object, Object> restMap = new HashMap<Object, Object>();
			UserInfo user = usersService.findUsersById(uid);
			// 获取该用户所有模块
			 // 创建模块集合 获取用户的所有角色
			List<Modules> moduleList = new ArrayList<Modules>();
			for(roles r:user.getRoleList()) {
				for(Modules m:r.getModules()) {  
					if(!moduleList.contains(m)) {//去重
						moduleList.add(m);
					}
				}
			}
			// 获取该用户的模块树
			List<Treemodule> modulesTree = modulesService.findUserModules(moduleList);
			
			if(user.getUname()!=null) { 
				restMap.put("tree", modulesTree); 
				restMap.put("code", "0");
				restMap.put("message", "成功");
			}else {
				restMap.put("code", "1");
				restMap.put("message", "失败");
			}
			return restMap;
		}
		/**
		 * 查询所有角色
		 * @return
		 */
		@GetMapping("/getall1")
		public List<roles> findAll() {
			System.out.println("查询所有角色");
			List<roles> r = rolesService.findAllRoles();
			return r; 
		}
		/**
		 * 根据用户id查看该用户角色
		 */
		@GetMapping("/getuseridfindRoles")
		public List<roles> findusers(String uid) {
			System.out.println("根据用户id查看用户角色"+uid);
			Optional<UserInfo> u = rolesService.findByIDfindRoles(uid);
			List<roles> d = u.get().getRoleList();
			System.out.println("d.size()"+d.size());
			return d;

		}
		
		
		/**
		 * 给用户添加角色
		 * @param userid
		 * @param roleid
		 */
		@PostMapping("/AddUserToRole")
		public void AddUserToRole(String uid,String rid){
			System.out.println("uid-----------------------"+uid);
			System.out.println("rid-----------------------"+rid);
			System.out.println(uid+"----------------------------");
			System.out.println(rid+"----------------------------");
			
			usersService.saveUserrole(uid, rid);
			
		}
		/**
		 * 给用户删除角色
		 * @param uId
		 * @param rId
		 */
		
		@RequestMapping("/delUserToRole")
		public void delUserToRole(String uid,String rid){
			System.out.println(uid+"----------------------------");
			System.out.println(rid+"----------------------------");
			usersService.delUserrole(uid, rid);
			
		}
			
}
